/*
 *  This file is part of "SchoolChoice",
 *  a school registration system.
 *
 *  Copyright (C) 2009 Nicolas Decerf, Olivier Jacquemin.
 *
 *  "SchoolChoice" is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  "SchoolChoice" is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program: see the file COPYING.txt.  If not, see
 *  http://www.gnu.org/licenses/.
 */

/*
 * Controller class.
 */

/*global SchoolChoice */

(function() {
     // Private static data.

     // Shortcut notation.
     var WAC = SchoolChoice.WebAppClient;

     var loginEditor;
     var schoolsList;
     var preferredSchools;
     var schoolEditor;
     var newUserEditor;
     var userProfileEditor;

     // Private static methods.
     function login_Callback(data, error) {
         if (error) {
             WAC.Logger.severe("Error received after invoking UserService.login(). " + error.msg);
             return;
         }
         WAC.Logger.info("UserService.login() invoked successfully.");
	     $("#LoginForm").dialog("close");

         if (loginEditor.getId() === "Admin") {
             showSectionForAdmin();
         }
         else {
             showSectionForChoice();
         }
     }

     function login_Click() {
	     $("#LoginForm").dialog("open");
     }

     function saveSchool_Callback(data, error) {
         if (error) {
             WAC.Logger.severe("Error received after invoking SchoolsService.saveSchool(). " + error.msg);
             return;
         }
         WAC.Logger.info("SchoolsService.saveSchool() invoked successfully.");
         $("#NewSchool").dialog("close");
     }

     function createUser_Callback(data, error) {
         if (error) {
             WAC.Logger.severe("Error received after invoking UserService.createUser(). " + error.msg);
             return;
         }
         WAC.Logger.info("UserService.createUser() invoked successfully.");
         $("#NewUser").dialog("close");
         showSectionForChoice();
     }

     function createUser_Click() {
         $("#NewUser").dialog("open");
     }

     function search_Callback(data, error) {
         if (error) {
             WAC.Logger.severe("Error received after invoking SchoolsService.search(). " + error.msg);
             return;
         }
         WAC.Logger.info("SchoolsService.search() invoked successfully, returning " +
                         data.length + " result(s).");

         schoolsList.setSchools(data);
     }

     function search_Click() {
         var criteria = $("#criteria").val();
         WAC.WebServiceProxy.SchoolsService.search(search_Callback, criteria);
     }

     function getPreferredSchools_Callback(data, error) {
         if (error) {
             WAC.Logger.severe("Error received after invoking SchoolsService.getPreferredSchools(). " + error.msg);
             return;
         }
         WAC.Logger.info("SchoolsService.getPreferredSchools() invoked successfully, returning " +
                         data.length + " result(s).");

         preferredSchools.setSchools(data);
     }

     function getPreferredSchools_Click() {
         WAC.WebServiceProxy.SchoolsService.getPreferredSchools(getPreferredSchools_Callback);
     }

     function edit_Click() {
         var school = schoolsList.getSelectedSchool();
         schoolEditor.setSchool(school);
         $("#NewSchool").dialog("open");
     }

     function getProfile_Callback(data, error) {
         if (error) {
             WAC.Logger.severe("Error received after invoking UserService.getProfile(). " + error.msg);
             return;
         }
         WAC.Logger.info("UserService.getProfile() invoked successfully.");

         userProfileEditor.setUserProfile(data);
     }

     function getProfile_Click() {
	     $("#UserProfile").dialog("open");
         WAC.WebServiceProxy.UserService.getProfile(getProfile_Callback);
     }

     function setProfile_Callback(data, error) {
         if (error) {
             WAC.Logger.severe("Error received after invoking UserService.setProfile(). " + error.msg);
             return;
         }
         WAC.Logger.info("UserService.setProfile() invoked successfully.");
	     $("#UserProfile").dialog("close");
     }

     function setPreferredSchoolsIds_Callback(data, error) {
         if (error) {
             WAC.Logger.severe("Error received after invoking SchoolsService.setPreferredSchoolsIds(). " + error.msg);
             return;
         }
         WAC.Logger.info("SchoolsService.setPreferredSchoolsIds() invoked successfully.");
     }

     function setPreferredSchools_Click() {
         var preferredIds = preferredSchools.getOrderedSchoolsIds();
         WAC.WebServiceProxy.SchoolsService.setPreferredSchoolsIds(setPreferredSchoolsIds_Callback, preferredIds);
     }

     function showWelcomeSection() {
         $("#WelcomeSection").show();
         $("#SchoolsSection").hide();
         $("#Menu").hide();
     }

     function showSectionForChoice() {
         $("#WelcomeSection").hide();
         $("#SchoolsSection").show();
         $("#Menu").show();
         $("#Edit").parents("li").hide();
         $("#GetPreferredSchools").parents("li").show();
         $("#SetPreferredSchools").parents("li").show();
     }

     function showSectionForAdmin() {
         $("#WelcomeSection").hide();
         $("#SchoolsSection").show();
         $("#Menu").show();
         $("#Edit").parents("li").show();
         $("#GetPreferredSchools").parents("li").hide();
         $("#SetPreferredSchools").parents("li").hide();
     }

     // Class and constructor declaration.
     WAC.Controller = function() { };

     // Public static methods
     WAC.Controller.load = function() {
         showWelcomeSection();

         $("#Log").dialog({
             width: 800
         });

         loginEditor = new WAC.LoginEditor();
         loginEditor.load("LoginForm");
         $("#LoginForm").dialog({
             autoOpen: false,
		     width: 550,
             modal: true,
		     buttons: {
			     OK: function() { 
                     WAC.WebServiceProxy.UserService.login(
                         login_Callback, loginEditor.getId(), loginEditor.getPassword());
			     },
			     Annuler: function() { 
				     $(this).dialog("close"); 
			     }
		     }
	     });

         schoolsList = new WAC.SchoolsList();
         schoolsList.load("SchoolsFound");

         preferredSchools = new WAC.SchoolsList();
         preferredSchools.load("PreferredSchools");

         $("#SchoolsFound, #PreferredSchools").sortable({
             connectWith: '.connectedSortable',
             update: function() { preferredSchools.refreshOrderedSchoolsIds(); }
		 }).disableSelection();

         schoolEditor = new WAC.SchoolEditor();
         schoolEditor.load("NewSchool");
         $("#NewSchool").dialog({
             autoOpen: false,
		     width: 600,
             modal: true,
		     buttons: {
			     OK: function() {
                     var school = schoolEditor.getSchool();
                     WAC.WebServiceProxy.SchoolsService.saveSchool(saveSchool_Callback, school);
			     },
			     Annuler: function() { 
				     $(this).dialog("close"); 
			     }
		     }
	     });


         newUserEditor = new WAC.NewUserEditor();
         newUserEditor.load("NewUser");
         $("#NewUser").dialog({
             autoOpen: false,
		     width: 500,
             modal: true,
		     buttons: {
			     "Ajouter l'élève": function() { 
                     if (newUserEditor.isPasswordValid()) {
                         var id = newUserEditor.getId();
                         var password = newUserEditor.getPassword();
                         WAC.WebServiceProxy.UserService.createUser(createUser_Callback, id, password);
                     }
                     else {
                         alert("Le mot de passe et la confirmation ne sont les mêmes !");
                     }
			     },
			     Annuler: function() { 
				     $(this).dialog("close"); 
			     }
		     }
	     });


         userProfileEditor = new WAC.UserProfileEditor();
         userProfileEditor.load("UserProfile");
         $("#UserProfile").dialog({
             autoOpen: false,
		     width: 600,
             modal: true,
		     buttons: {
			     OK: function() {
                     var userProfile = userProfileEditor.getUserProfile();
                     WAC.WebServiceProxy.UserService.setProfile(setProfile_Callback, userProfile);
			     },
			     Annuler: function() { 
				     $(this).dialog("close"); 
			     }
		     }
	     });

         $("#Login").click(login_Click);
         $("#Logout").click(showWelcomeSection);
         $("#Search").click(search_Click);
         $("#GetPreferredSchools").click(getPreferredSchools_Click);
         $("#Edit").click(edit_Click);
         $("#CreateUser").click(createUser_Click);
         $("#GetProfile").click(getProfile_Click);

         $("#SetPreferredSchools").click(setPreferredSchools_Click);
     };
 }) ();
